cd ${work}5_reg_analysis

use input\sample_idt, clear

*PROMOTION
destring jurnr, gen(jurnr_num)
merge 1:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist *_1 {
    replace `v'=0 if `v'==.
}

*COLLAPSE TO FIRM-YEAR
collapse (max) treated_1 tdcinitiative_1 , by(jurnr year)

*INDUSTRY CODES
merge 1:1 jurnr year using input\firm_it, assert(2 3) keep(3) keepusing(g36) nogen

*COLLAPSE + RESHAPE
collapse (mean) xTreated=treated_1 xCalled=tdcinitiative_1 (count) xObservations=treated_1 , by(g36)	
replace xTreated=xTreated*100
replace xCalled=xCalled*100
reshape long x , i(g36) j(stat) string

*LABEL
levelsof g36, local(shortlist)
local namelist `" "Food products, etc." "Textiles, etc." "Wood, paper, etc." "Oil refinery, etc." "Chemicals" "Pharmaceuticals" "Plastic, etc." "Basic metals, etc." "Electronic components" "Electrical equipment" "Machinery" "Transport equipment" "Furniture, etc." "'
local n=1
foreach x of local shortlist {
	local y : word `n' of `namelist'
	replace g36 = "`y'" if g36=="`x'"
	local ++n
}

*ORGANIZE	
gen temp1=x if stat=="Observations"
bysort g36 : egen temp2=mean(temp1)

gen temp3=1 if stat=="Treated"
replace temp3=2 if stat=="Called"
replace temp3=3 if stat=="Observations"

gsort -temp2 temp3
drop temp?	

gen n=_n
replace n=n[_n-1] if stat=="Observations"
labmask n, values(g36)

*MANUALLY DELETE STATISTICS THAT DO NOT COMPLY WITH DST'S RULES ON DATA CONFIDENTIALITY
replace x = . if g36=="Oil refinery, etc." & (stat=="Treated" | stat=="Called")
	
*FIGURE	
qui sum n
twoway ///
	bar x n if stat=="Treated" , yaxis(1) || ///
	bar x n if stat=="Called" , yaxis(1) || ///
	line x n if stat=="Observations" , yaxis(2) ///
	ytitle("Percent", axis(1)) ytitle("Observations", axis(2)) ///
	legend(order(1 "Treated" 2 "Called" 3 "Observations")) ///
	xtitle("") xlabel(2(3)`r(max)', valuelabel noticks angle(90)) ///
	yscale(range(0 30) axis(1)) ylabel(0(10)30, axis(1))
graph export output\figure_a1.png, replace	
graph close _all